package com.school.service.impl;

import com.school.service.TestService;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.stereotype.Service;

import java.util.Properties;

@Service
public class TesServiceImpl implements TestService {
    @Override
    public String testMail(String mail) {
        String from = "lmwwxcoj@cqjtuoj.com"; // 发件人邮箱（Postfix 自定义发件人域名）
        String host = "110.42.62.203"; // 你的云服务器 IP

        // 配置 SMTP 属性
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "false");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.starttls.required", "true");

        // 创建邮件会话
        Session session = Session.getInstance(properties);

        try {
            // 创建邮件内容
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));
            //设置邮箱标题
            message.setSubject("小虫OJ官方验证码");
            // HTML格式的邮件模板，验证码加粗居中显示
            String htmlMsgTemplate = "<!DOCTYPE html>" +
                    "<html>" +
                    "<head>" +
                    "<meta charset='UTF-8'>" +
                    "<style>" +
                    "body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }" +
                    ".container { max-width: 600px; margin: 0 auto; padding: 20px; }" +
                    ".header { text-align: center; margin-bottom: 30px; }" +
                    ".logo { width: 120px; height: auto; margin-bottom: 15px; }" +
                    ".title { font-size: 24px; font-weight: bold; color: #333; margin: 0; }" +
                    ".code { font-size: 24px; font-weight: bold; color: #007bff; text-align: center; " +
                    "background-color: #f8f9fa; padding: 15px; margin: 20px 0; border-radius: 5px; " +
                    "border: 2px solid #007bff; }" +
                    ".footer { margin-top: 30px; font-size: 12px; color: #666; text-align: center; }" +
                    "</style>" +
                    "</head>" +
                    "<body>" +
                    "<div class='container'>" +
                    "<div class='header'>" +
                    "<img src='https://www.xiaochongoj.cn/cqjtuoj/OJLogo.webp' alt='小虫OJ Logo' class='logo'>" +
                    "<h2 class='title'>小虫OJ验证码</h2>" +
                    "</div>" +
                    "<p>您好！</p>" +
                    "<p>您的验证码是：</p>" +
                    "<div class='code'>{CODE}</div>" +
                    "<p><strong>请勿泄露给其他人。</strong></p>" +
                    "<div class='footer'>" +
                    "来自小虫OJ官方: <a href='https://www.xiaochongoj.cn'>https://www.xiaochongoj.cn</a>" +
                    "</div>" +
                    "</div>" +
                    "</body>" +
                    "</html>";

            // 设置邮件内容为HTML格式
            message.setContent(htmlMsgTemplate.replace("{CODE}", "523516"), "text/html; charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException mex) {
            System.err.println("邮件发送失败：" + mex.getMessage());
        }
        return "success";
    }
}

